<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <div>
        <el-dialog :visible.sync="show" title="下载小程序源码包" width="600px" v-loading="loading" :show-close="false"
            :close-on-click-modal="false" :close-on-press-escape="false" element-loading-text="正在打包源代码，请等待!">
            <el-form label-position="top" label-width="200px">
                <el-form-item label="请先填写服务端绑定的域名，并开启https" required>
                    <el-input placeholder="请填写小程序名称" v-model="api_host" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" icon="el-icon-close" @click="show = false">取消</el-button>
                <el-button size="small" icon="el-icon-download" type="primary" :disabled="license_is_free"
                    @click="downloadWeappSourceCode">下载源代码</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { initWeapp } from "@/api/myconfig";
import { downloadWeappCode } from "@/api/myconfig";
import {
    getLicenseInfo,
} from "@/api/licenseinfo";
import { computed } from "vue";
export default {
    components: {},
    computed: {},
    mounted() {
        this.api_host = this.$store.state.myconfig.api_host;
        getLicenseInfo().then((resp) => {
            if (resp.code != 0) {
                return;
            }
            this.licenseInfo = resp.data;
        });
    },
    computed: {
        license_is_free() {
            if (this.licenseInfo && this.licenseInfo.only_read_area && (this.licenseInfo.only_read_area.NAME == '2' || this.licenseInfo.only_read_area.NAME == '3')) {
                return false;
            }
            return true;
        },
    },
    data() {
        return {
            api_host: "",
            show: false,
            loading: false,
            licenseInfo: null,
        };
    },
    methods: {
        downloadWeappSourceCode: function () {
            //没有绑定域名或没有设置https域名，就弹出提示文字：请先绑定域名并配置开启https协议
            console.log(this.api_host);
            if (!this.api_host || this.api_host.indexOf("https://") == -1) {
                this.$message.error("请先绑定域名并配置开启https协议");
                return;
            }

            this.loading = true;
            downloadWeappCode({
                api_host: this.api_host,
            }).then((resp) => {
                this.loading = false;
                const blob = new Blob([resp], { type: "application/zip" });
                // 创建一个a标签
                const link = document.createElement("a");
                link.href = URL.createObjectURL(blob); // 设置文件的URL
                link.download = "app.zip"; // 设置文件的下载名称

                // 模拟用户点击链接进行下载
                link.click();

                // 释放URL对象
                URL.revokeObjectURL(link.href);
                this.$message.success("下载成功");

                this.show = false;
            })
                .finally(() => {
                    this.loading = false;
                });
        },
        showdialog() {
            this.show = true;
        },
    },
};
</script>

<style scoped>
.toast {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.toast i {
    color: #e29c40;
    font-size: 96px;
}

.toast-text {
    width: 400px;
    color: #e29c40;
    font-size: 14px;
    text-align: center;
    margin: 40px auto;
}
</style>